import React, { Component } from 'react'
import { Modal, Button, Input, Form, Select, message } from 'antd'
import { accountAdd } from '../../api/user'
import { findRoles } from '../../api/role'
let { Option } = Select

export default class Add extends Component {
  state = {
    isModalVisible: false,
    data: [],
  }
  showModal = () => {
    this.setState({
      isModalVisible: true,
    })
  }

  handleCancel = () => {
    this.setState({
      isModalVisible: false,
    })
  }
  componentDidMount() {
    this.getAllRoles()
  }

  // 获取所有角色
  getAllRoles = () => {
    findRoles().then((res) => {
      this.setState({ data: res.data.data })
      // console.log(res.data.data)
    })
  }
  onFinish = (values) => {
    accountAdd(values).then((lcz) => {
      this.props.add()
      message.success(lcz.data.msg)
    })
    this.setState({
      isModalVisible: false,
    })
    // console.log(values)
  }
  render() {
    let { isModalVisible, data } = this.state
    return (
      <>
        <Button onClick={this.showModal}>添加用户</Button>
        <Modal
          destroyOnClose
          title="添加用户"
          visible={isModalVisible}
          onCancel={this.handleCancel}
          footer={null}
        >
          <Form
            name="normal_login"
            className="login-form"
            onFinish={this.onFinish}
            initialValues={{}}
          >
            <Form.Item
              label="用户名"
              name="account"
              rules={[
                {
                  required: true,
                  message: '请输入用户名!',
                },
              ]}
            >
              <Input placeholder="用户名" />
            </Form.Item>
            <Form.Item
              label="密码"
              name="password"
              rules={[
                {
                  required: true,
                  message: '请输入密码!',
                },
              ]}
            >
              <Input placeholder="邮箱地址" />
            </Form.Item>
            <Form.Item
              label="邮箱"
              name="email"
              rules={[
                {
                  required: true,
                  message: '请输入密码!',
                },
              ]}
            >
              <Input placeholder="权限" />
            </Form.Item>
            <Form.Item
              label="权限"
              name="role"
              rules={[
                {
                  required: true,
                  message: '请输入密码!',
                },
              ]}
            >
              <Select>
                {data.map((v) => {
                  return (
                    <Option key={v._id} values={v._id}>
                      {v.name}
                    </Option>
                  )
                })}
              </Select>
            </Form.Item>

            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
                className="login-form-button"
              >
                添加
              </Button>
            </Form.Item>
          </Form>
        </Modal>
      </>
    )
  }
}
